﻿using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Text;
using System.Collections;

/// <summary>
/// 文章内容分页
/// </summary>
public class CArticleSplitPage
{
    public CArticleSplitPage()
    {
        
    }

    #region 成员
    protected string articleContent = string.Empty; //文章内容
    protected int splitType = 0;   //分页方式(0.不分页 1.自动 2.手动)
    protected int wordnum = 2000;    //自动分页字量
    protected const string SplitSign = "[NextPage]";    //手动分页标记
    const char splitChar = '№';  //拆分字符  
    #endregion

    #region 属性
    /// <summary>
    /// 文章内容
    /// </summary>
    public string ArticleContent
    {
        set { articleContent = value; }        
    }
    /// <summary>
    /// 分页类型
    /// </summary>
    public int SplitType
    {
        set { splitType = value; }
    }
    /// <summary>
    /// 分页字量
    /// </summary>
    public int WordNumber
    {
        set { wordnum = value; }
    }    
    #endregion

    #region 方法
    /// <summary>
    /// 处理结果
    /// </summary>
    /// <returns>html加js组合</returns>
    public string SplitResult()
    {
        ArrayList splitcoll = new ArrayList();  //拆分文字集合
        //拆分文字
        switch (splitType)
        { 
            case 0:
                return articleContent;
                break;
            case 1:   //自动
                int len = (int)Math.Ceiling((double)articleContent.Length / (double)wordnum);
                for (int i = 0; i < len; i++)
                {
                    int msn = wordnum;
                    int last = articleContent.Length - i * wordnum;
                    if (last < wordnum) { msn = last; }

                    splitcoll.Add(articleContent.Substring(i * wordnum, msn));
                }
                break;
            case 2:   //手动
                articleContent = articleContent.Replace(splitChar.ToString(), string.Empty);    //先去掉人为 [编程标记№]
                articleContent = articleContent.Replace(SplitSign, splitChar.ToString());       //再将 [分页标记] 换成 [编程标记№]
                splitcoll.AddRange(articleContent.Split(splitChar));
                break;
        }
        //组合
        return UniteHtml(splitcoll);
    }

    /// <summary>
    /// 组合分页
    /// </summary>    
    private string UniteHtml(ArrayList result)
    {
        StringBuilder htmltext = new StringBuilder();
        //内容
        StringBuilder chead = new StringBuilder();
        for (int i = 0; i < result.Count; i++)
        {
            chead.Append("\r\n\r\n<!-- 第[").Append(i + 1).Append("]页 开始 -->")
                .Append("\r\n<table border='0' width='100%' id='page_").Append(i + 1).Append("' class='article_content' ").Append((i == 0) ? string.Empty : "style='display:none;'").Append(">")
                .Append("\r\n\t<tr><td>\r\n")
                .Append("\r\n").Append(result[i])
                .Append("\r\n\r\n\t</td></tr>")
                .Append("\r\n</table>")
                .Append("\r\n<!-- 第[").Append(i + 1).Append("]页 结束 -->");
        }
        //脚本
        htmltext.Append("<div>")
        .Append(chead)
        .Append("\r\n<!-- 分页导航 开始 -->")
        .Append("\r\n<div class='splitpage' style='text-align:center;'>")
        .Append("\r\n\t<span id='lbl_frist' style='cursor:pointer;' onclick='GoToPageIndex(1);'>首页</span>&nbsp;")
        .Append("\r\n\t<span id='lbl_pre' style='cursor:pointer;' onclick='GoToPageIndex(--CurrentIndex);'>上一页</span>&nbsp;")
        .Append("\r\n\t<span id='lbl_currindex' style='color:red;'>1</span>&nbsp;")
        .Append("\r\n\t<span id='lbl_next' style='cursor:pointer;' onclick='GoToPageIndex(++CurrentIndex);'>下一页</span>&nbsp;")
        .Append("\r\n\t<span id='lbl_last' style='cursor:pointer;' onclick='GoToPageIndex(CountPages);'>尾页(").Append(result.Count).Append(")</span>")
        .Append("\r\n</div>")
        .Append("\r\n<!-- 分页导航 结束 -->")
        .Append("\r\n</div>")
        .Append("\r\n<script type='text/javascript'>")
        .Append("\r\nvar CountPages = ").Append(result.Count).Append(";	//总页数")
        .Append("\r\nvar CurrentIndex = 1; //当前索引页")
        .Append("\r\n//跳转页")
        .Append("\r\nfunction GoToPageIndex(pageindex)")
        .Append("\r\n{")
        .Append("\r\n\t//页面索引必顺大于或等于1，同时小于或等于[总页数]")
        .Append("\r\n\tif(pageindex >= 1 && pageindex <= CountPages)")
        .Append("\r\n\t{")
        .Append("\r\n\t\tCurrentIndex = pageindex;")
        .Append("\r\n\t\tvar pageid = 'page_';	//标记前缀")
        .Append("\r\n\t\t//显示指定页")
        .Append("\r\n\t\tfor(i = 1; i <= CountPages; i++)")
        .Append("\r\n\t\t{")
        .Append("\r\n\t\t\tvar singlePage = document.getElementById(pageid + i);")
        .Append("\r\n\t\t\tif(typeof(singlePage) != 'undefined') { singlePage.style.display = (i == CurrentIndex) ? '' : 'none'; }")
        .Append("\r\n\t\t}")
        .Append("\r\n\t\t//分页导航改变")
        .Append("\r\n\t\tsplitnav();")
        .Append("\r\n\t}")
        .Append("\r\n}")
        .Append("\r\nfunction splitnav()")
        .Append("\r\n{")
        .Append("\r\n\t//分页导航改变")
        .Append("\r\n\tdocument.getElementById('lbl_frist').style.display = (CurrentIndex == 1) ? 'none' : '';")
        .Append("\r\n\tdocument.getElementById('lbl_pre').style.display = (CurrentIndex == 1) ? 'none' : '';")
        .Append("\r\n\tdocument.getElementById('lbl_next').style.display = (CurrentIndex == CountPages) ? 'none' : '';")
        .Append("\r\n\tdocument.getElementById('lbl_last').style.display = (CurrentIndex == CountPages) ? 'none' : '';")
        .Append("\r\n\tdocument.getElementById('lbl_currindex').innerHTML = CurrentIndex;")
        .Append("\r\n}")
        .Append("\r\nsplitnav();")
        .Append("\r\n</script>");

        return htmltext.ToString();
    }
    #endregion
}
